<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/public/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/public/css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div id="ID-transfer-demo-showSearch"></div>

        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:src="@{/public/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['jquery','transfer','form'], function () {
        let $ = layui.jquery;
        let transfer = layui.transfer;
        let form = layui.form;
        const roleId = '[[${roleId}]]';
        let transferVos = '[[${transferVos}]]'.replace(/&quot;/g, '"');
        transferVos  = JSON.parse(transferVos);
        console.log(transferVos);
        const selected = []
        // 数据
        transferVos.forEach(vo => {
            if(vo.checked){
                selected.push(vo.value)
            }
        });
        // 渲染
        transfer.render({
            id: 'demo-inst',
            elem: '#ID-transfer-demo-showSearch',
            data: transferVos,
            title: ['未选用户', '已选用户'],
            showSearch: true,
            value: selected,
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let checkedDatas = transfer.getData('demo-inst'); // 获取右侧数据
            let ids = [];
            //获取菜单ID
            checkedDatas.forEach(checkedData => ids.push(checkedData.value))
            $.post("/system/role/save_role_users",{"userIds":ids,"roleId":roleId}, function (res) {
                if (res.code === 200) {
                    layer.msg("设置权限成功!", {icon: 1, time: 1000}, function () {
                        let iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                } else {
                    layer.msg(res.msg, {icon: 2, time: 1000});
                }
            });

            return false;
        });


    });
</script>

</body>
</html>